package com.batch.quartz.job;

import org.quartz.*;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.configuration.JobLocator;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <h3>partitionjob</h3>
 * <p>继承 quartzJobBean</p>
 * QuartzJobLauncher
 *
 * @author : hduong
 * @version : 1.0
 * @date : 2019-12-20 16:43
 **/
@Component
public class ImportUserQuartzJob extends QuartzJobBean {

/*-------------方式一：获取jobName、jobLauncher和jobLocator*/
//  private String jobName;
//  private JobLauncher jobLauncher;
//  private JobLocator jobLocator;
//
//  public String getJobName() {
//      return jobName;
//  }
//
//  public void setJobName(String jobName) {
//      this.jobName = jobName;
//  }
//
//  public JobLauncher getJobLauncher() {
//      return jobLauncher;
//  }
//
//  public void setJobLauncher(JobLauncher jobLauncher) {
//      this.jobLauncher = jobLauncher;
//  }
//
//  public JobLocator getJobLocator() {
//      return jobLocator;
//  }
//
//  public void setJobLocator(JobLocator jobLocator) {
//      this.jobLocator = jobLocator;
//  }

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
//        JobDetail jobDetail = context.getJobDetail();
        Scheduler scheduler = context.getScheduler();
//        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        try {
            SchedulerContext schedulerContext = scheduler.getContext();
            String jobName = schedulerContext.getString("jobName");
            JobLauncher jobLauncher = (JobLauncher) schedulerContext.get("jobLauncher");
            JobLocator jobLocator = (JobLocator) schedulerContext.get("jobLocator");
            System.out.println("jobName : " + jobName);
            System.out.println("jobLauncher : " + jobLauncher);
            System.out.println("jobLocator : " + jobLocator);
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = sf.format(new Date());
            System.out.println("Current Time : " + date);
            Job job = jobLocator.getJob(jobName);
            if (job != null) {
                /*启动spring batch的批处理作业*/
                JobExecution jobExecution = jobLauncher.run(job,
                        new JobParametersBuilder().addDate("date", new Date()).toJobParameters());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
//        String jobName = jobDataMap.getString("jobName");
//        JobLauncher jobLauncher = (JobLauncher) jobDataMap.get("jobLauncher");
//        JobLocator jobLocator = (JobLocator) jobDataMap.get("jobLocator");

/*-----------------------------方式二获取结束---------------------------------*/

    }
}
